package com.example.shop.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.shop.mapper.AuthMapper;
import com.example.shop.mapper.UserAuthMapper;
import com.example.shop.pojo.Auth;
import com.example.shop.pojo.User;
import com.example.shop.pojo.UserAuth;
import com.example.shop.service.UserAuthService;

@Service
public class UserAuthServiceImpl implements UserAuthService {

	private static final String NICKNAME_ROOT = "root";
	private static final String NICKNAME_USER = "user";
	private static final String NICKNAME_ADMIN = "admin";
	@Autowired
	private UserAuthMapper userAuthMapper;
	@Autowired
	private AuthMapper authMapper;

	/**
	 * 赋予员工权限
	 */
	@Override
	public String giveAuth(UserAuth userAuth, List<UserAuth> giveUserAuths) {
		// 检测该账号是否有赋予权限
		List<UserAuth> uas = userAuthMapper.selectAllGiveAuthByAccount(userAuth);
		
		String s= checkIsAuth(giveUserAuths,uas);
		if(!"true".equals(s)) {
			return s;
		}
//		for (UserAuth giveUserAuth : giveUserAuths) {
//			Long auth_id = giveUserAuth.getAuthId();
//			boolean result = false;
//			for (UserAuth ua : uas) {
//				if (auth_id != null && ua.getAuthId() != null && auth_id.equals(ua.getAuthId())) {
//					result = true;
//					giveUserAuth.setAuthClass(ua.getAuthClass());
//					break;
//				}
//			}
//			if (result == false) {
//				authMapper.selectByAuthId(auth_id);
//				return "该用户没有该权限：" + auth_id;
//			}
//		}
		// 赋予权限
		for (UserAuth giveUserAuth : giveUserAuths) {
			userAuthMapper.addUserAuth(giveUserAuth);
		}
		return "赋予权限成功";
	}

	/**
	 * 赋予员工对应身份权限
	 */
	@Override
	public String giveBaseAuth(User user) {
		// 获取员工权限身份
		String nickname = user.getNickname();
		if (nickname == null && user.getAccount() == null) {
			return "员工身份信息不完整";
		}
		// 赋予员工对应身份权限
		if (nickname.equals(NICKNAME_ROOT)) {
			// 赋予root权限
			ArrayList<UserAuth> al = new ArrayList<>();
			al.add(new UserAuth(null, user.getAccount(), 1L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 2L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 3L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 4L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 5L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 6L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 7L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 8L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 9L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 10L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 11L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 12L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 13L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 13L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 14L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 15L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 16L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 17L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 18L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 19L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 20L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 21L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 22L, 3L));
			for (UserAuth a : al) {
				userAuthMapper.addUserAuth(a);
			}
		} else if (nickname.equals(NICKNAME_ADMIN)) {
			// 赋予admin权限
			ArrayList<UserAuth> al = new ArrayList<>();
			al.add(new UserAuth(null, user.getAccount(), 1L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 2L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 3L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 4L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 5L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 6L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 7L, 3L));
			al.add(new UserAuth(null, user.getAccount(), 8L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 9L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 10L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 11L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 12L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 13L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 13L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 14L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 15L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 16L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 17L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 18L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 20L, 1L));
			for (UserAuth a : al) {
				userAuthMapper.addUserAuth(a);
			}
		} else {
			// 赋予user权限
			ArrayList<UserAuth> al = new ArrayList<>();
			al.add(new UserAuth(null, user.getAccount(), 2L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 6L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 7L, 1L));
			al.add(new UserAuth(null, user.getAccount(), 9L, 1L));
			for (UserAuth a : al) {
				userAuthMapper.addUserAuth(a);
			}
		}
		return " 员工获取对应身份权限成功";
	}

	/**
	 * 权限校验
	 */
	@Override
	public String checkAuth(List<UserAuth> checkAuths) {
		// 根据account获取用户所有权限
		List<UserAuth> uas = userAuthMapper.selectAllByAccount(checkAuths.get(0));
		String s= checkIsAuth(checkAuths,uas);
		if(!"true".equals(s)) {
			return s;
		}
		return "权限验证通过";
	}
	
	/**
	 * 检验是否有权限
	 * @param checkAuths 检查的用户权限
	 * @param uas 用户所有权限
	 * @return 有 就返回"true"，反之返回 "该用户没有该权限：" + auth_id
	 */
	private String checkIsAuth(List<UserAuth> checkAuths,List<UserAuth> uas) {
				// 检查是否有权限
				for (UserAuth giveUserAuth : checkAuths) {
					Long auth_id = giveUserAuth.getAuthId();
					boolean result = false;
					for (UserAuth ua : uas) {
						if (auth_id != null && ua.getAuthId() != null && auth_id.equals(ua.getAuthId())) {
							result = true;
							giveUserAuth.setAuthClass(ua.getAuthClass());
							break;
						}
					}
					if (result == false) {
						Auth auth = authMapper.selectByAuthId(auth_id);
						return "该用户没有该权限：" +"权限id:"+ auth_id+"  权限名称:"+auth.getAuthName();
					}
				}
				return "true";
	}

}
